using System;
using System.Collections.Generic;
using System.Text;

namespace KukaDataCom
{
    public class CommandJob
    {

        #region Eigenschaften

        private readonly Modus modus;
        public Modus Modus
        {
            get { return modus; }
        }

        private readonly Int16[] ints;
        public Int16[] Ints
        {
            get { return ints; }
        }

        private readonly char[] chars;
        public char[] Chars
        {
            get { return chars; }
        }

        private readonly float[] floats;
        public float[] Floats
        {
            get { return floats; }
        }

        #endregion

        #region Konstruktor

        public CommandJob(Modus modus, Int16[] ints, char[] chars, float[] floats)
        {
            if (modus == null) throw new ArgumentNullException("modus");
            if (ints == null) throw new ArgumentNullException("ints");
            if (chars == null) throw new ArgumentNullException("chars");
            if (floats == null) throw new ArgumentNullException("floats");

            this.modus = modus;
            this.floats = floats;
            this.chars = chars;
            this.ints = ints;
        }

        #endregion

        #region toByteArray

        public byte[][] toByteArray()
        {
            List<byte[]> result = new List<byte[]>();

            if (Modus.IntCount > 0)
            {
                List<byte> tempBytes = new List<byte>();
                Int16 empty = 0;

                for (int i = 0; i < Modus.IntCount; i++)
                {
                    if (i < ints.Length)
                        tempBytes.AddRange(BitConverter.GetBytes(Ints[i]));
                    else
                        tempBytes.AddRange(BitConverter.GetBytes(empty));
                }

                result.Add(tempBytes.ToArray());
            }
            if (Modus.CharCount > 0)
            {
                byte[] tempBytes;
                char empty = ' ';

                if (Modus.CharCount >= Chars.Length)
                    tempBytes = Encoding.ASCII.GetBytes(Chars, 0, Modus.CharCount);
                else
                {
                    char[] tempChars = new char[Modus.CharCount];
                    Chars.CopyTo(tempChars, 0);
                    for (int i=Chars.Length; i<modus.CharCount; i++)
                        tempChars[i] = empty;
                    tempBytes = Encoding.ASCII.GetBytes(tempChars);
                }

                result.Add(tempBytes);
            }
            if (Modus.FloatCount > 0)
            {
                const int maximaleAnzahlFloatsProPaket = 20;

                List<byte> tempBytes = new List<byte>();
                float empty = 0;
                for (int i=0; i<Modus.FloatCount; i++)
                {
                    if (i < Floats.Length)
                        tempBytes.AddRange(BitConverter.GetBytes(Floats[i]));
                    else
                        tempBytes.AddRange(BitConverter.GetBytes(empty));

                    if ((i+1)%maximaleAnzahlFloatsProPaket == 0)
                    {
                        result.Add(tempBytes.ToArray());
                        tempBytes.Clear();
                    }
                }
                if (tempBytes.Count > 0)
                    result.Add(tempBytes.ToArray());
            }

            return result.ToArray();
        }

        #endregion

    }
}